package com.mj.mapper;

import com.mj.domain.ActionBean;
import com.mj.domain.ClassificationBean;
import com.mj.domain.RuleBean;
import com.mj.domain.RuleResultBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by SXY on 2016/1/5.
 */
public interface Mapper {
    @Select("select * from action_info where action_id=#{id}")
    ActionBean getOneAction(int id);

    @Select("select * from rule_info where action_id=#{id}")
    RuleBean getOneRule(int id);

    @Select("select * from action_info where classification_id=#{classification_id} ORDER BY sort_order")
    List<ActionBean> getGroupActions(int classification_id);

    @Select("select * from action_info,action_classification where action_info.classification_id = action_classification.classification_id ORDER BY sort_order")
    List<ActionBean> getAllAction();

    @Select("select *  from rule_result_info where oid = ( select oid from rule_result_info where rule_id = #{id}  ORDER BY exec_time DESC LIMIT 1) ORDER BY sort")
    List<RuleResultBean> getNewestRuleResultByRuleId(int id);

    @Select("select rule_id from rule_info where action_id = #{id}")
    List<Integer> getRuleIdsByActionId(int id);

    @Select("select * from action_classification ORDER BY classification_id")
    List<ClassificationBean> getAllClassification();
//    类别的id是自增的，不同于actionId、ruleId
    @Insert("insert into action_classification(classification_name) value (#{classification_name})")
    void setOneClassification(ClassificationBean classificationBean);

    @Insert("insert into action_info value (#{action_id},#{action_name},#{classification_id},#{path},#{notes},#{graph_type},#{graph_info},#{exec_type},#{interal_time},#{status},#{last_exectime},#{sort_order})")
    void setOneAction(ActionBean bean);

    @Insert("insert into rule_info value (#{rule_id},#{action_id},#{rule_type},#{rule_value},#{timestamp},#{status},#{hbase_table_name},#{time_type},#{interval_time},#{cols})")
    void setOneRule(RuleBean bean);

    @Select("SELECT action_id FROM action_info ORDER BY action_id DESC LIMIT 1")
    int getLastActionId();

    @Select("SELECT rule_id FROM rule_info ORDER BY rule_id DESC LIMIT 1")
    int getLastRuleId();

    @Select("SELECT sort_order FROM action_info ORDER BY sort_order DESC LIMIT 1")
    int getLastSortNum();

    @Select("SELECT classification_id FROM action_classification WHERE classification_name = #{classification_name} LIMIT 1")
    int getClassificationIdByName(String classification_name);

    @Update(("UPDATE rule_info SET rule_type = #{rule_type},rule_value = #{rule_value},timestamp = #{timestamp},hbase_table_name = #{hbase_table_name},time_type = #{time_type},interval_time = #{interval_time},cols = #{cols} WHERE rule_id = #{rule_id}"))
    void updateOneRule(RuleBean bean);

    @Update(("UPDATE action_info SET action_name = #{action_name},classification_id = #{classification_id},path = #{path},notes = #{notes},graph_type = #{graph_type},graph_info = #{graph_info},exec_type = #{exec_type},interal_time = #{interal_time} WHERE action_id = #{action_id}"))
    void updateOneAction(ActionBean bean);
}
